package edu.je.patent.controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.junqson.je.common.model.ApiResult;
import edu.je.patent.model.PantentModel;
import edu.je.patent.repo.PantentRepo;
import edu.je.patent.service.PattenService;
import edu.je.patent.util.PageUtil;
import org.hibernate.annotations.CreationTimestamp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author MengXin
 * @description:
 * @program: je-gateway
 * @date 2021/4/24 8:59
 * @mail mx@radish.cloud
 */


@RestController
@RequestMapping("pantent")
public class PantentController {
    
    @Autowired
    public PattenService pattenService;

    /**
     * @param
     * @return com.junqson.je.common.model.ApiResult
     * @author Mengxin
     * @describe:
     * @date 2021/4/24 20:04
     */
    @RequestMapping("getPatentListBy")
    public ApiResult getPantentListBy(@RequestParam(required = false) String title,
                                      @RequestParam(required = false) String applicant,
                                      @RequestParam(required = false) String abstractInfo,
                                      @RequestParam(required = false) String country,
                                      @RequestParam(required = false) Integer page,
                                      @RequestParam(required = false) Integer size,
                                      @RequestParam(required = false) Long oid

    ){

        PantentModel pantentModel = new PantentModel();
        pantentModel.setTitle(title);
        pantentModel.setAbstractInfo(abstractInfo);
        pantentModel.setCountry(country);
        pantentModel.setApplicant(applicant);
        pantentModel.setPid(oid);
        Sort sort = Sort.by(Sort.Direction.DESC, "pid");
        Pageable pageable = PageUtil.getPageable(page-1, size, sort);
        List<PantentModel> pantentModelList = pattenService.getPantentByDao(pantentModel,pageable).getContent();
        return ApiResult.success(pantentModelList);
    }
    
    /**
     * @param oid
	 * @param title
	 * @param country
	 * @param applyTime
	 * @param applicant
	 * @param abstractInfo
	 * @param publiseTime
     * @return com.junqson.je.common.model.ApiResult
     * @author Mengxin
     * @describe: 
     * @date 2021/4/26 21:33
     */
    @RequestMapping("addPatent")
    public ApiResult addNews(@RequestParam("oid") String oid,
                             @RequestParam("title") String title,
                             @RequestParam("country") String country,
                             @RequestParam("applytime") String applyTime,
                             @RequestParam("applicant") String applicant,
                             @RequestParam("AbstractInfo") String abstractInfo,
                             @RequestParam("publishTime")@DateTimeFormat(pattern="yyyy-MM-dd") Date publiseTime
                             ){
        PantentModel pantentModel = pattenService.addPantent(oid,title,country,applyTime,applicant,abstractInfo,publiseTime);
        return ApiResult.success(pantentModel);

    }

    @RequestMapping("getAllCountry")
    public ApiResult getAllcountry(@RequestParam(required = false)@DateTimeFormat(pattern="yyyy-MM-dd") Date applyTime,
                                   @RequestParam(required = false)@DateTimeFormat(pattern="yyyy-MM-dd") Date publishdTiem){
        return ApiResult.success(pattenService.getAllcountry(applyTime,publishdTiem));
    }

    @RequestMapping("getAllApplicant")
    public ApiResult getAllapplicant(@RequestParam(required = false)@DateTimeFormat(pattern="yyyy-MM-dd") Date applyTime,
                                     @RequestParam(required = false)@DateTimeFormat(pattern="yyyy-MM-dd") Date publishdTiem){
        return ApiResult.success(pattenService.getApplicant(applyTime,publishdTiem));
    }

    @RequestMapping("getPatentById")
    public ApiResult getPatentById(Long id){
        return ApiResult.success(pattenService.getPantentById(id));
    }

    @RequestMapping("getAll")
    public ApiResult getAll(@RequestParam(required = false)Integer page,
                            @RequestParam(required = false)Integer limit){
        return ApiResult.success(pattenService.getAll(page-1,limit));
    }


    @RequestMapping("deletePatentByid")
    public ApiResult deletePatentById(Long id){
        PantentModel pantentModel = pattenService.deletePaten(id);
        if (pantentModel!=null){
            return ApiResult.success(pantentModel);
        }else {
            return ApiResult.error();
        }
    }

    @RequestMapping("getAllTime")
    public ApiResult getAllTim(@RequestParam(required = false)Long less,
                                   @RequestParam(required = false)Long more){
        return ApiResult.success(pattenService.serachTime(less,more));
    }




}
